package org.tpdata.api.specialdiseasedata.tianta;

import asserts.Asserts;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.yamlbeans.YamlException;
import common.Data;
import common.RunCaseJson;
import common.SetUpTearDown1;
import exceptions.TestNGRetry;
import io.qameta.allure.Allure;
import io.qameta.allure.Feature;
import io.qameta.allure.Story;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.Test;
import utils.MyFilter;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;
import static common.Login.login;
import static common.TestStep.requestAndRespondBody;


@Feature("数据检索-titan筛选-获取符合条件的病例")
public class getpatientlist  {

    Logger log = Logger.getLogger(getpatientlist.class);
    public static final String system = "env.properties";
    Data data = login();

    business.specialdiseasedata.CreateTemplate ct=new business.specialdiseasedata.CreateTemplate();
    String td;
    {
        try {
            td = ct.CreateModel(0);
        } catch (ConfigurationException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (YamlException e) {
            e.printStackTrace();
        }
    }

    @Story("获取符合条件的病例")
    @Test(retryAnalyzer = TestNGRetry.class,description = "获取符合条件的病例")
    public void runCase() {

        JSONObject json = new JSONObject();
//        Map<String,String > json=new HashMap<String, String>();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("create_id", data.getUserId());
        json.put("page", 1);
        json.put("size", -1);
        json.put("templateId", td);

        log.info("请求报文："+json.toString());
        Response response = RunCaseJson.runCase(json, "get", "api-sourcedata/dmp/template/result/list");
        String jsonStr = response.asString();
        log.info("响应报文：" + jsonStr);
        String url = RestAssured.baseURI + ":" + RestAssured.port + RestAssured.basePath + "api-sourcedata/dmp/template/result/list";
        requestAndRespondBody(url, json, jsonStr);

        JSONObject obj = JSONObject.parseObject(jsonStr);
        JSONObject  jb=obj.getJSONObject("data").getJSONObject("dataPage");
        int numberOfElements=jb.getIntValue("numberOfElements");

        Allure.addAttachment("判断依据","判断numberOfElements的值不为空");
        Assert.assertNotEquals(numberOfElements,"");
//        Assert.assertEquals(flag, Contants.SUCCESS);
        Allure.addAttachment("判断依据","判断content数组里age和sex的值不为空");
        if (numberOfElements > 0) {
            JSONArray jsonArray = jb.getJSONArray("content");
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Map<String, Object> jsonMap = JSONObject.toJavaObject(jsonObject, Map.class);
                List a = MyFilter.getLikeByMap(jsonMap, "AGE_");
                String age = a.get(0).toString();
//            String age=jsonObject.getString("AGE_6");

                List b = MyFilter.getLikeByMap(jsonMap, "SEX_");
                String SEX = b.get(0).toString();
                Assert.assertNotEquals(age, "");
                Assert.assertNotEquals(SEX, "");
            }
        }else {
            log.info("病例集为0");
        }

    }

    @Test(retryAnalyzer = TestNGRetry.class,description = "比较筛选结果中病历占比是否一致")
    //符合条件的 共 519份病历， 占总数55.51%， 共265个患者， 占总数66.75%
    public void runCase1()  {
        JSONObject json = new JSONObject();
//        Map<String,String > json=new HashMap<String, String>();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("create_id", data.getUserId());
        json.put("templateId", td);

        log.info(json.toString());
        Response response = RunCaseJson.runCase(json, "get", "api-sourcedata/dmp/template/result/count");
        String jsonStr = response.asString();
        log.info("响应报文：" + jsonStr);

        JSONObject obj = JSONObject.parseObject(jsonStr);
        JSONArray jsonArray=obj.getJSONArray("data");
        JSONObject jb =jsonArray.getJSONObject(0);
        JSONArray ja=jb.getJSONArray("list");

        //获取list第一个数据
        JSONObject object1=ja.getJSONObject(0);
        String count1=object1.getString("count");
        String total1=object1.getString("total");
        String scale1 = object1.getString("scale");


       //将sting 格式转换成float
        float a=Float.parseFloat(count1);
        float b=Float.parseFloat(total1);
        float k=(float)a/b*100;
        //取2位小数
        java.math.BigDecimal   big   =   new   java.math.BigDecimal(k);
        String  l = big.setScale(2,java.math.BigDecimal.ROUND_HALF_UP).doubleValue() +"%";

        String url = RestAssured.baseURI + ":" + RestAssured.port + RestAssured.basePath + "api-sourcedata/dmp/template/result/count";
        requestAndRespondBody(url, json, jsonStr);
        Assert.assertEquals(scale1,l);

       //获取list第二个数据
        JSONObject object2=ja.getJSONObject(1);
        String count2=object2.getString("count");
        String total2=object2.getString("total");
        String scale2 = object2.getString("scale");

       //将sting 格式转换成float
        float c=Float.parseFloat(count2);
        float d=Float.parseFloat(total2);
        float m=(float)c/d*100;
        //取2位小数
        java.math.BigDecimal   second   =   new   java.math.BigDecimal(m);
        String  s = second.setScale(2,java.math.BigDecimal.ROUND_HALF_UP).doubleValue() +"%";
        Assert.assertEquals(scale2,s);


/*
//筛选来源：来自0位住院患者的0份病历
     for(int i=0;i<jsonArray.size();i++){
         JSONObject jb1 =jsonArray.getJSONObject(i);
         JSONArray ja1=jb1.getJSONArray("list");
         for(int j=0;j<ja1.size();j++){
             JSONObject object=ja.getJSONObject(j);
             Assert.assertNotEquals(object.getString("total"),"");
         }
     }
*/

    }

}
